//
//  HomeCell.swift
//  微博
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

class HomeCell: UITableViewCell {
    
    
    private var homeBottomConstraint : Constraint?
    //添加数据属性
    var status:Status? {
        didSet{
            headCell.status = status
            //判断是否是转发微博
            //卸载约束
            self.homeBottomConstraint?.uninstall()
            if let retweetStatus = status?.retweeted_status {
                //设置约束
                bottomCell.snp_updateConstraints { (make) -> Void in
                    //设置底部约束
                   self.homeBottomConstraint = make.top.equalTo(retweetedCell.snp_bottom).constraint
                }
                retweetedCell.retweeted = retweetStatus
                retweetedCell.hidden = false
            }else{
                bottomCell.snp_updateConstraints { (make) -> Void in
                    //设置底部约束
                    self.homeBottomConstraint = make.top.equalTo(headCell.snp_bottom).constraint
                }
                retweetedCell.hidden = true
            }
        }
    }
    
    
    
   //创建构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置UI
        setupUI()
        //设置选中的样式
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件
    private func setupUI() {
        //添加控件
        contentView.addSubview(headCell)
        contentView.addSubview(retweetedCell)
        contentView.addSubview(bottomCell)
        contentView.addSubview(lineCell)
        lineCell.backgroundColor = UIColor.lightGrayColor()
        
        lineCell.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
            make.height.equalTo(10)
        }
        
        //设置约束
        headCell.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(lineCell.snp_bottom)
            make.left.right.equalTo(contentView)
            //设置行高
//            make.bottom.equalTo(contentView.snp_bottom)
        }
        //设置转发约束
        retweetedCell.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headCell.snp_bottom).offset(StatusCellMarigin)
            make.left.right.equalTo(contentView)
//            make.height.equalTo(50)
        }
        
        
        //设置约束
        bottomCell.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(self)
            //设置底部约束
           self.homeBottomConstraint = make.top.equalTo(retweetedCell.snp_bottom).constraint
            make.height.equalTo(40)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(bottomCell.snp_bottom)
        }
        
    }
    
    //懒加载控件
    private lazy var lineCell:UIView = UIView()
    private lazy var headCell:HeadCellView = HeadCellView()
    private lazy var bottomCell:BottomCellView = BottomCellView()
    private lazy var retweetedCell:RetweetCellView = RetweetCellView()

}
